package com.csgc.camera.biz.vip

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder

import com.csgc.camera.bean.VipType

import com.csgc.camera.util.clickWithTrigger
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ItemVipTypeBinding
import com.hlyj.http.base.tool.lib_hlyj_base.camera.ShopInfoBean


class VipTypeAdapter(val context: Context,val list:ArrayList<ShopInfoBean> = ArrayList()):Adapter<VipTypeAdapter.VipTypeHolder>() {

    private var buyPosition = 0
    //永久会员
    var foreverPosition = -1
    private val defaultColor:Int by lazy {
        ContextCompat.getColor(context, R.color.color_333333)
    }
    private val checkColor:Int by lazy {
        ContextCompat.getColor(context, R.color.color_AF6D4B)
    }
    fun updateData(data: List<ShopInfoBean?>?) {
        list.clear()
        // 过滤掉 null 值并转换为非空类型
        val filteredData = data?.filterNotNull() ?: emptyList()
        list.addAll(filteredData)
        notifyDataSetChanged()
    }
    var listener:OnVipTypeClickListener?=null
    fun getCheckedProductId():Int{
        return list[buyPosition].id
    }
    fun getCheckedPrice():Double{
        return list[buyPosition].currentPrice
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VipTypeHolder {
        return VipTypeHolder(ItemVipTypeBinding.inflate(LayoutInflater.from(context),parent,false))
    }

    override fun onBindViewHolder(holder: VipTypeHolder, @SuppressLint("RecyclerView") position: Int) {
        val data = list[position]
        holder.binding.apply {
            if (data.type == "1"){
                foreverPosition = position
            }
            tvVipType.text = data.name
            tvSalePrice.text = "${data.currentPrice}"
            tvOriginPrice.text = "原价"+ "${data.originPrice}"


            tvOriginPrice.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
            tvOriginPrice.paint.isAntiAlias = true


            root.isSelected = buyPosition == position
            if (buyPosition == position){
                tvVipType.setTextColor(checkColor)
            }else{
                tvVipType.setTextColor(defaultColor)
            }
            root.clickWithTrigger {
                buyPosition = position
                notifyDataSetChanged()
                listener?.onItemClick(data.currentPrice)
            }

        }

    }

    override fun getItemCount(): Int {
        return list.size
    }

    class VipTypeHolder(val binding:ItemVipTypeBinding):ViewHolder(binding.root)

    interface OnVipTypeClickListener{
        fun onItemClick(price:Double)
    }
}